/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import gui.form.OwnerPanel;
import gui.form.PetPanel;
import gui.form.VeterinarianPanel;
import gui.search.OwnerSearchPane;
import gui.search.PetSearchPane;
import gui.search.VetSearchPane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import Model.Owner;
import Model.Pet;
import Model.Veterinarian;

import dao.DaoOwner;
import dao.DaoPet;
import dao.DaoVeterinarian;

/**
 *
 * @author Juan Ortiz
 */
public class main extends javax.swing.JFrame {

    /**
     * Creates new form main
     */
	
	private static main main;
	
	public static main getMain(){
		if(main == null)
			main = new main();
		
		return main;
	}
	
    private main() {
    	setResizable(false);
        initComponents();
    }

    private void showContentPanel (JPanel panel){
        reslutPanel.removeAll();
        
        reslutPanel.add(panel);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        searchTextField = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        checkboxOwner = new javax.swing.JCheckBox();
        checkboxPet = new javax.swing.JCheckBox();
        checkboxVeterinarian = new javax.swing.JCheckBox();
        reslutPanel = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        btnEditVeterinarian = new javax.swing.JButton();
        btnEditVeterinarian.setHorizontalAlignment(SwingConstants.LEADING);
        btnEditOwner = new javax.swing.JButton();
        btnEditPet = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Healthy Pets");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Search"));

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        checkboxOwner.setText("Owner");

        checkboxPet.setText("Pet");

        checkboxVeterinarian.setText("Veterinarian");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(115, 115, 115)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(searchTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(checkboxOwner)
                        .addGap(18, 18, 18)
                        .addComponent(checkboxVeterinarian)
                        .addGap(18, 18, 18)
                        .addComponent(checkboxPet)))
                .addGap(18, 18, 18)
                .addComponent(btnSearch)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(checkboxOwner)
                    .addComponent(checkboxVeterinarian)
                    .addComponent(checkboxPet))
                .addContainerGap())
        );

        reslutPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Results"));
        
        tabbedPane = new JTabbedPane(JTabbedPane.TOP);

        javax.swing.GroupLayout reslutPanelLayout = new javax.swing.GroupLayout(reslutPanel);
        reslutPanelLayout.setHorizontalGroup(
        	reslutPanelLayout.createParallelGroup(Alignment.TRAILING)
        		.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 596, Short.MAX_VALUE)
        );
        reslutPanelLayout.setVerticalGroup(
        	reslutPanelLayout.createParallelGroup(Alignment.TRAILING)
        		.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
        );
        
        tabbedPane.addTab("Owner", holderOwner);
        tabbedPane.addTab("Veterinarian", holderVeterinarian);
        tabbedPane.addTab("Pet", holderPet);
        reslutPanel.setLayout(reslutPanelLayout);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Add/Edit Iinformation"));

        btnEditVeterinarian.setText("Veterinarian");
        btnEditVeterinarian.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditVeterinarianActionPerformed(evt);
            }
        });

        btnEditOwner.setText("Owner");
        btnEditOwner.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditOwnerActionPerformed(evt);
            }
        });

        btnEditPet.setText("Pet");
        btnEditPet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditPetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3Layout.setAutoCreateGaps(true);
        jPanel3Layout.setHorizontalGroup(
        	jPanel3Layout.createParallelGroup(Alignment.TRAILING)
        		.addGroup(jPanel3Layout.createSequentialGroup()
        			.addGap(81)
        			.addComponent(btnEditOwner, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.RELATED, 91, Short.MAX_VALUE)
        			.addComponent(btnEditVeterinarian)
        			.addGap(99)
        			.addComponent(btnEditPet, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
        			.addGap(69))
        );
        jPanel3Layout.setVerticalGroup(
        	jPanel3Layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(jPanel3Layout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(jPanel3Layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(btnEditVeterinarian)
        				.addComponent(btnEditPet)
        				.addComponent(btnEditOwner))
        			.addContainerGap(15, Short.MAX_VALUE))
        );
        jPanel3.setLayout(jPanel3Layout);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        layout.setHorizontalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(layout.createParallelGroup(Alignment.LEADING)
        				.addComponent(reslutPanel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
        				.addComponent(jPanel1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
        				.addComponent(jPanel3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        			.addContainerGap())
        );
        layout.setVerticalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addGap(5)
        			.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(reslutPanel, GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(jPanel3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        			.addGap(56))
        );
        getContentPane().setLayout(layout);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnEditOwnerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditOwnerActionPerformed
            addOwnerPane(new OwnerPanel());    
    }//GEN-LAST:event_btnEditOwnerActionPerformed

    private void btnEditVeterinarianActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditVeterinarianActionPerformed
    	addVeterianarianPane(new VeterinarianPanel());
    }//GEN-LAST:event_btnEditVeterinarianActionPerformed

    private void btnEditPetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditPetActionPerformed
    	addPetPane(new PetPanel());
    }//GEN-LAST:event_btnEditPetActionPerformed

	private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnSearchActionPerformed
		if (checkboxOwner.isSelected()) {
			final List<Owner> ownersList;
			DaoOwner owner = new DaoOwner();
			ownersList = owner.findByName(searchTextField.getText());

			if (ownersList.isEmpty()) {
//				JOptionPane.showMessageDialog(null, "Owner with name "
//						+ searchTextField.getText() + " not found");

			} else {
				OwnerSearchPane p = new OwnerSearchPane();
				p.fillTable(ownersList);
				p.addTable();
				addOwnerPane(p);

			}

		}

		if (checkboxPet.isSelected()) {
			final List<Pet> petsList;
			DaoPet pets = new DaoPet();
			petsList = pets.findByName(searchTextField.getText());

			if (petsList.isEmpty()) {
//				JOptionPane.showMessageDialog(null, "Pet with name "
//						+ searchTextField.getText() + " not found");

			} else {

				PetSearchPane p = new PetSearchPane();
				p.fillTable(petsList);
				addPetPane(p);
			}

		}

		if (checkboxVeterinarian.isSelected()) {
			final List<Veterinarian> vetsList;
			DaoVeterinarian vetDao = new DaoVeterinarian();
			vetsList = vetDao.findByName(searchTextField.getText());

			if (vetsList.isEmpty()) {
//				JOptionPane.showMessageDialog(null, "Pet with name "
//						+ searchTextField.getText() + " not found");

			} else {

				VetSearchPane p = new VetSearchPane();
				p.fillTable(vetsList);

				addVeterianarianPane(p);
			}
		}
	}//GEN-LAST:event_btnSearchActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
        	String nativeLF = UIManager.getSystemLookAndFeelClassName();
            /*
        	for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
            */
        	javax.swing.UIManager.setLookAndFeel(nativeLF);
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                main.getMain().setVisible(true);
            }
        });
    }
    
    void addPanel(JPanel p){    	
		reslutPanel.removeAll();
		reslutPanel.revalidate();
		reslutPanel.setLayout(new BorderLayout());                        
        reslutPanel.add(p, BorderLayout.CENTER);
        reslutPanel.revalidate();
        reslutPanel.repaint();
    }
    
    public void addVeterianarianPane(JPanel panel){
    	holderVeterinarian.removeAll();
    	holderVeterinarian.add(panel);
    	tabbedPane.repaint();
    }
    
    public void addOwnerPane(JPanel panel){
    	holderOwner.removeAll();
    	holderOwner.add(panel);
    	tabbedPane.repaint();
    }

    public void addPetPane(JPanel panel){
    	holderPet.removeAll();
    	holderPet.add(panel);
    	tabbedPane.repaint();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditOwner;
    private javax.swing.JButton btnEditPet;
    private javax.swing.JButton btnEditVeterinarian;
    private javax.swing.JButton btnSearch;
    private javax.swing.JCheckBox checkboxOwner;
    private javax.swing.JCheckBox checkboxPet;
    private javax.swing.JCheckBox checkboxVeterinarian;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JPanel reslutPanel;
    private javax.swing.JTextField searchTextField;
    private JTabbedPane tabbedPane;
    private JPanel holderOwner = new JPanel();
    private JPanel holderVeterinarian  = new JPanel();
    private JPanel holderPet  = new JPanel();
}
